package com.lemon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lemon.domain.*;
import com.lemon.domain.dto.EntryDetailDto;
import com.lemon.domain.dto.SkuDto;
import com.lemon.mapper.*;
import com.lemon.service.WarehousingEntryDetailsSortService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.service.WarehousingEntryDetailsService;
@Service
@RequiredArgsConstructor
public class WarehousingEntryDetailsServiceImpl extends ServiceImpl<WarehousingEntryDetailsMapper, WarehousingEntryDetails> implements WarehousingEntryDetailsService{
    private final WarehousingEntryMapper warehousingEntryMapper;
    private final WarehousingEntryDetailsMapper entryDetailsMapper;
    private final UserMapper userMapper;
    private final WarehousingEntryDetailsSortMapper detailsSortMapper;
    private final SkuMapper skuMapper;
    private final SpuMapper spuMapper;
    private final SupplierMapper supplierMapper;

    @Override
    public PageResult getEntryDetailsPage(Integer page, Integer pageSize, Integer status) {
        PageHelper.startPage(page, pageSize);
        List<WarehousingEntryDetails> entryDetailsList = entryDetailsMapper.selectList(Wrappers.<WarehousingEntryDetails>lambdaQuery().eq(WarehousingEntryDetails::getStatus, status));
        if (status==3){
            List<WarehousingEntryDetails> list = entryDetailsMapper.selectList(Wrappers.<WarehousingEntryDetails>lambdaQuery().eq(WarehousingEntryDetails::getStatus, status + 1));
            entryDetailsList.addAll(list);
        }
        List<EntryDetailDto> list = new ArrayList<>();
        entryDetailsList.forEach(entryDetails -> {
            EntryDetailDto entryDetailDto = new EntryDetailDto();
            entryDetailDto.setId(entryDetails.getId());
            WarehousingEntry entry = warehousingEntryMapper.selectById(entryDetails.getWarehousingEntryId());
            entryDetailDto.setConsignorId(Math.toIntExact(entry.getConsignorId()));
            entryDetailDto.setConsignorName(entry.getConsignorName());
            entryDetailDto.setBatchNumber(entry.getBatchNumber());
            entryDetailDto.setSupplierId(Math.toIntExact(entryDetails.getSupplierId()));
            entryDetailDto.setSupplierName(entryDetails.getSupplierName());
            entryDetailDto.setPredictedArrivalTime(entryDetails.getPredictedArrivalTime());
            entryDetailDto.setRealArrivalTime(entryDetails.getRealArrivalTime());
            entryDetailDto.setSkuId(entryDetails.getSkuId());
            entryDetailDto.setTitle(entryDetails.getTitle());
            entryDetailDto.setCode(entryDetails.getCode());
            entryDetailDto.setStatus(entryDetails.getStatus());
            List<WarehousingEntryDetailsSort> sorts = detailsSortMapper.selectList(Wrappers.<WarehousingEntryDetailsSort>lambdaQuery().eq(WarehousingEntryDetailsSort::getWarehousingEntryDetailsId, entryDetails.getId()));
            Integer count = 0;
            if (CollectionUtils.isNotEmpty( sorts)){
                for (WarehousingEntryDetailsSort sort : sorts) {
                    count += sort.getCount();
                    count -= sort.getDamageCount();
                }
            }
            entryDetailDto.setCount(count);
            list.add(entryDetailDto);
        });

        // 修改Page转换方法 - 正确的方式
        Page<EntryDetailDto> entryDetailDtoPage = new Page<>();
        entryDetailDtoPage.addAll(list);
        entryDetailDtoPage.setPageNum(page);
        entryDetailDtoPage.setPageSize(pageSize);
        // 设置总数，这里需要从查询结果中获取
        Page<?> pageInfo = (Page<?>) entryDetailsList;
        entryDetailDtoPage.setTotal(pageInfo.getTotal());

        return new PageResult(entryDetailDtoPage.getTotal(), entryDetailDtoPage.getResult());
    }

    @Override
    public List<WarehousingEntryDetails> getList(Integer id) {
        List<WarehousingEntryDetails> list = entryDetailsMapper.selectList(Wrappers.<WarehousingEntryDetails>lambdaQuery().eq(WarehousingEntryDetails::getWarehousingEntryId, id));
        return list;
    }

    @Override
    public List<SkuDto> getEntry(Integer id) {
        List<WarehousingEntryDetails> list = entryDetailsMapper.selectList(Wrappers.<WarehousingEntryDetails>lambdaQuery().eq(WarehousingEntryDetails::getWarehousingEntryId, id));
        List<SkuDto> skuDtos = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)){
            list.forEach(entryDetails -> {
                SkuDto skuDto = new SkuDto();
                Sku sku = skuMapper.selectById(entryDetails.getSkuId());
                skuDto.setId(sku.getId());
                skuDto.setCode(sku.getCode());
                Spu spu = spuMapper.selectById(sku.getSpuId());
                skuDto.setTitle(spu.getTitle());
                skuDto.setSupplierId(spu.getSupplierId());
                skuDto.setSupplierName(supplierMapper.selectById(spu.getSupplierId()).getSupplierName());
                skuDtos.add(skuDto);
            });
        }
        return skuDtos;
    }

}
